#ifndef METAITEM_H
#define METAITEM_H


#include "data_global.h"


#include <QObject>
#include <QPointer>
#include <QMetaType>
#include <QSharedData>
#include <QSharedDataPointer>
#include <QExplicitlySharedDataPointer>

class MetaData;


// XML: Element is the class name, arguments are meta property name/value pairs
class DATASHARED_EXPORT MetaItem : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString field READ field WRITE setField)
    Q_PROPERTY(int index READ index WRITE setIndex)
    Q_PROPERTY(QString optionRef READ optionRef WRITE setOptionRef)
    Q_PROPERTY(int editType READ editType WRITE setEditType)
    Q_PROPERTY(bool editable READ editable WRITE setEditable)

    class Data : public QSharedData
    {
    public:
        Data()
        {
            index = -1;
            editType = -1;
            editable = true;
        }

        Data(const Data& other) : QSharedData(other)
        {
            field = other.field;
            index = other.index;
            optionRef = other.optionRef;
            editType = other.editType;
            editable = other.editable;
        }

        QString field;

        int index;

        QString optionRef;

        int editType;
        bool editable;

    };

public:

    enum EditType
    {
        EditType_NONE = 0,
        EditType_SINGLE,
        EditType_MULTI
    };

    Q_INVOKABLE MetaItem(MetaData *parent = 0);
    Q_INVOKABLE MetaItem(const QString& field, MetaData *parent = 0);
    Q_INVOKABLE MetaItem(const QString& optionRef, const QString& field, MetaData *parent = 0);
    MetaItem(const MetaItem& other);

    QString field() const;
    void setField(const QString& field);

    int index() const;
    void setIndex(int index);

    QString optionRef() const;
    void setOptionRef(const QString& optionRef);

    int editType() const;
    void setEditType(int editType);

    bool editable() const;
    void setEditable(bool enable = true);

    void setParent(QObject *parent);


protected:

    // Actually I'm not sure if explicitly shared data is correct...
    QExplicitlySharedDataPointer<Data> d;
//    QSharedDataPointer<Data> d;


};

Q_DECLARE_METATYPE(MetaItem*)

typedef QPointer<MetaItem> MetaItemPtr;


#endif // METAITEM_H
